package com.qiaofang.collectmanage.analysis;

import org.apache.commons.lang3.StringUtils;
import org.jsoup.nodes.Element;

/**
 * Created with IDEA
 * 此类方法值只允许，新增，不允许修改 ，修改需要review
 *
 * @author:ZJ.J
 * @Date:2018/6/1
 * @Time:14:28
 */
public class ReturnElementHandle {


    /**
     * 根据方法名称执行ReturnElement对应的方法
     *
     * @param lastElement 最新html element
     * @param args        args[0]方法名称
     * @return 返回html element
     */
    public static Element executeMethodByMethodName(Element lastElement, String... args) {
        Element retrunElement = lastElement;
        String methodName = args[0];
        for (flow t : flow.values()) {
            if (StringUtils.isNotBlank(methodName) && methodName.trim().equals(t.getMethodName())) {
                retrunElement =  t.handle(retrunElement, args);
            }
        }
        return retrunElement;
    }

    /**
     * 定义枚举类
     */
    enum flow {
        //给枚举自定义方法
        SELECT("select") {
            @Override
            public Element handle(Element element, String... args) {
                return ReturnElement.select(element, args);
            }
        },
        SELECTLIST("selectList") {
            @Override
            public Element handle(Element element, String... args) {
                return ReturnElement.selectList(element, args);
            }
        },

        REPLACE("replace") {
            @Override
            public Element handle(Element element, String... args) {
                return ReturnElement.replace(element, args);
            }
        };
        /**
         * 枚举属性
         */
        private final String methodName;

        flow(String methodName) {
            this.methodName = methodName;
        }

        public String getMethodName() {
            return methodName;
        }

        public abstract Element handle(Element element, String... args);
    }
}
